import {ActionContext} from 'vuex';
import ListModule from '@/store/modules/list-module';
import ListState from '@/store/modules/list-state';
import FlowScheme from '@/store/entities/flow/flow-scheme/flow-scheme';
import Ajax from '@/lib/ajax';
import PageResult from '@/store/entities/page-result';

interface FlowSchemeState extends ListState<FlowScheme> {
    flowSchemeForEdit:any,
    excelFile:any,
    flowScheme:any,
}

class FlowSchemeModule extends ListModule<FlowSchemeState, any, FlowScheme> {
    state = {
        totalCount: 0,
        currentPage: 1,
        pageSize: 10,
        list: new Array<FlowScheme>(),
        loading: false,
        flowSchemeForEdit:{},
        excelFile:{},
        flowScheme:{},
    };
    actions = {
        async obtainFlowSchemes(context:ActionContext<FlowSchemeState, any>,payload:any){
            context.state.loading=true;
            const response=await Ajax.post('/api/services/app/FlowScheme/ObtainFlowSchemes',payload.data);
            context.state.loading=false;
            const result=response.data.result as PageResult<FlowScheme>;
            context.state.totalCount=result.totalCount;
            context.state.list=result.items;
        },

        async createOrUpdateFlowScheme(context:ActionContext<FlowSchemeState, any>,payload:any){
            await Ajax.post('/api/services/app/FlowScheme/CreateOrUpdateFlowScheme',payload.data);
        },
        async deleteFlowScheme(context:ActionContext<FlowSchemeState, any>,payload:any){
            await Ajax.delete('/api/services/app/FlowScheme/DeleteFlowScheme?Id='+ payload.data.id);
        },
        async getFlowSchemeForEdit(context:ActionContext<FlowSchemeState, any>,payload:any){
            const response=await Ajax.get('/api/services/app/FlowScheme/GetFlowSchemeForEdit?Id=' + payload.data.id);
            context.state.flowSchemeForEdit=response.data.result;
        },
        async getFlowScheme(context:ActionContext<FlowSchemeState, any>,payload:any){
            const response=await Ajax.get('/api/services/app/FlowScheme/GetFlowScheme?Id='+payload.data.id);
            context.state.flowScheme=response.data.result;
        },
        async obtainFlowSchemesToExcel(context:ActionContext<FlowSchemeState, any>,payload:any){
            const response=await Ajax.post('/api/services/app/FlowScheme/ObtainFlowSchemesToExcel',payload.data);
            context.state.excelFile=response.data.result;
        },
    };
    mutations = {
        setCurrentPage(state: FlowSchemeState, page: number) {
            state.currentPage = page;
        },
        setPageSize(state: FlowSchemeState, pageSize: number) {
            state.pageSize = pageSize;
        },
    };
}

const flowSchemeModule = new FlowSchemeModule();
export default flowSchemeModule;
